<?php

/**
 * 栏目管理
 * File Name：CategoryController.php
 * File Encoding：UTF-8
 * File New Time：2014-5-5 17:21:51
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class CategoryController extends AdminBase {

    //栏目管理首页
    public function actionIndex() {
        if (IS_POST) {
            //修改栏目排序
            $listorders = $_POST['listorders'];
            if (!empty($listorders) && is_array($listorders)) {
                foreach ($listorders as $catid => $orid) {
                    $model = CategoryModel::model()->findByPk($catid);
                    if ($model) {
                        $model->listorder = $orid;
                        $model->save();
                    }
                }
            }
        }
        $tree = new Tree();
        $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        $catList = CategoryModel::model()->findAll(array('order' => 'listorder ASC'));
        //处理后的栏目列表
        $categorys = array();
        foreach ($catList as $rs) {
            $rs = $rs->attributes;
            $str_manage = array();
            $str_manage[] = '<a href="' . AdminBase::U("add", array("parentid" => $rs['cat_id'])) . '">添加子栏目</a>';
            $str_manage[] = '<a href="' . AdminBase::U("editor", array("cat_id" => $rs['cat_id'])) . '">编辑</a>';
            $str_manage[] = '<a href="' . AdminBase::U("delete", array("cat_id" => $rs['cat_id'])) . '" class="J_ajax_del">删除</a>';

            $rs['str_manage'] = implode(' | ', $str_manage);
            $categorys[$rs['cat_id']] = $rs;
        }
        $str = "<tr>
	<td align='center'><input name='listorders[\$cat_id]' type='text' size='3' value='\$listorder' class='input'></td>
	<td align='center'>\$cat_id</td>
	<td >\$spacer\$cat_name</td>
	<td align='center' >\$str_manage</td>
	</tr>";
        $tree->init($categorys);
        $categorydata = $tree->get_tree(0, $str);
        $this->assign("categorys", $categorydata);
        $this->render();
    }

    //添加栏目
    public function actionAdd() {
        if (IS_POST) {
            $model = new CategoryModel('add');
            $model->attributes = $_POST;
            if ($model->validate() && $model->save()) {
                $this->success('栏目添加成功！', AdminBase::U('index'));
            } else {
                $error = $model->getOneError();
                $this->error($error ? $error : '栏目添加失败！');
            }
        } else {
            if (!isset($_GET['parentid'])) {
                $_GET['parentid'] = 0;
            }
            $this->assign('parentid', (int) $_GET['parentid']);
            $this->render();
        }
    }

    public function actionEditor() {
        $cat_id = (int) Yii::app()->request->getParam('cat_id');
        if (empty($cat_id)) {
            $this->error('请指定需要编辑的栏目！');
        }
        $model = CategoryModel::model()->findByPk($cat_id);
        if (empty($model)) {
            $this->error('该栏目不存在！');
        }
        if (IS_POST) {
            //属性
            $_POST['is_best'] = isset($_POST['is_best']) ? (int) $_POST['is_best'] : 0;
            $_POST['is_new'] = isset($_POST['is_new']) ? (int) $_POST['is_new'] : 0;
            $_POST['is_hot'] = isset($_POST['is_hot']) ? (int) $_POST['is_hot'] : 0;
            $model->scenario = 'editor';
            $model->attributes = $_POST;
            //判断父栏目是否是本身栏目
            if ($model->parentid == $model->cat_id) {
                $model->parentid = 0;
            }
            if ($model->validate() && $model->save()) {
                $this->success('栏目修改成功！', AdminBase::U('index'));
            } else {
                $error = $model->getOneError();
                $this->error($error ? $error : '栏目修改失败！');
            }
        } else {
            $info = $model->attributes;
            $this->assign('info', $info);
            $this->render();
        }
    }

    //删除栏目
    public function actionDelete() {
        $cat_id = (int) Yii::app()->request->getParam('cat_id');
        if (empty($cat_id)) {
            $this->error('请指定需要删除的栏目！');
        }
        if (CategoryModel::model()->deleteCategoryByPk($cat_id)) {
            $this->success('栏目删除成功！');
        } else {
            $this->error('栏目删除失败！');
        }
    }

}
